import 'package:number_spark_flutter/config/ns_global_config.dart';
import 'package:number_spark_flutter/routes/ns_app_pages.dart';
import 'package:number_spark_flutter/style/ns_style.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:sp_util/sp_util.dart';

class NSAppView extends StatefulWidget {
  const NSAppView({Key? key}) : super(key: key);

  @override
  State<NSAppView> createState() => _NSAppViewState();
}

class _NSAppViewState extends State<NSAppView> {
  @override
  void initState() {
    super.initState();
    SpUtil.getInstance();
    PackageInfo.fromPlatform().then((value) {
      NSGlobalConfig.packageInfo = value;
    });
  }

  @override
  Widget build(BuildContext context) {
    final botToastBuilder = BotToastInit();
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      splitScreenMode: true,
      useInheritedMediaQuery: false,
      //ScreenUtil 5.8.1的bug  必须加这个属性 否则键盘弹出会遮挡输入框  fix 2023-7-20 升级到screenUtil 5.8.4后正常了
      builder: (_, child) => GetMaterialApp(
          // showPerformanceOverlay: true,//显示性能标签
          title: 'NumberSpark',
          builder: (context, child) {
            child = botToastBuilder(context, child);
            return child;
          },
          transitionDuration: const Duration(milliseconds: 100),
          defaultTransition: Transition.cupertino,
          navigatorObservers: [BotToastNavigatorObserver()],
          initialRoute: NSRoutes.splash,
          theme: ThemeData(
            scaffoldBackgroundColor: kAppBcgColor,
            hintColor: Colors.white, //提示文字颜色
            colorScheme: ColorScheme(
              primary: kAppColor('#FF93D452'),
              //激活状态下的颜色
              brightness: Brightness.light,
              onPrimary: Colors.transparent,
              secondary: Colors.grey,
              onSecondary: Colors.grey,
              error: Colors.red,
              onError: Colors.red,
              background: Colors.white,
              onBackground: Colors.white,
              surface: Colors.white,
              onSurface: kAppColor("#D0D8DA"), //非激活状态下的颜色
            ),
          ),
          getPages: NSAppPages.pages),
    );
  }
}

void hideKeyboard(BuildContext? context) {
  if (null != context) {
    FocusScopeNode currentFocus = FocusScope.of(context);
    if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
      FocusManager.instance.primaryFocus!.unfocus();
    }
  }
}
